﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myBoard.Framework.Controls.Navigation;
using Webnet.Controls;

namespace Webnet.BO.ConfigSteps
{
    public class SourceType : StepBase, IBreadcrumbExplorerEntity
    {
        private SourceTypeCtrl _sourceTypeCtrl;
        public BrowserContentType BrowserContentType { get; set; }
        

        #region IBreadcrumbExplorerEntity Members

        public void Dispose()
        {
            
        }
        

        public System.Windows.FrameworkElement GetControl()
        {
            if (_sourceTypeCtrl == null)
            {
                _sourceTypeCtrl = new SourceTypeCtrl();
                _sourceTypeCtrl._nextBt.Click += (a, b) =>
                    {
                        if (IBreadcrumbExplorerNextEntitySelected != null)
                        {
                            if (_sourceTypeCtrl._htmlTypeRadio.IsChecked == true)
                            {
                                Settings.ContentType = BrowserContentType.Html;
                                HtmlSourceType htmlSourceType = new HtmlSourceType();
                                htmlSourceType.Name = "HTML code";
                                htmlSourceType.Settings = Settings;
                                IBreadcrumbExplorerNextEntitySelected(this, htmlSourceType);
                            }
                            else if (_sourceTypeCtrl._urlTypeRadio.IsChecked == true)
                            {
                                Settings.ContentType = BrowserContentType.Url;
                                UrlSourceType urlSourceType = new UrlSourceType();
                                urlSourceType.Name = "Site url";
                                urlSourceType.Settings = Settings;
                                IBreadcrumbExplorerNextEntitySelected(this, urlSourceType);
                            }
                        }
                    };
                _sourceTypeCtrl._cancelBt.Click += (a, b) =>
                    {
                        RaiseWizardCanceled();
                    };
            }
            
            return _sourceTypeCtrl;
        }

        internal void RestoreSettings(Settings settings)
        {
            Settings = settings;
            if (_sourceTypeCtrl != null)
            {
                switch (settings.ContentType)
                {
                    case BrowserContentType.NotDefined:
                        {
                            _sourceTypeCtrl._htmlTypeRadio.IsChecked = false;
                            _sourceTypeCtrl._urlTypeRadio.IsChecked = false;                            
                        }
                        break;

                    case BrowserContentType.Html:
                        _sourceTypeCtrl._htmlTypeRadio.IsChecked = true;
                        if (IBreadcrumbExplorerNextEntitySelected != null)
                        {
                            HtmlSourceType htmlSourceType = new HtmlSourceType();
                            htmlSourceType.Settings = settings;
                            htmlSourceType.Name = "HTML";
                            htmlSourceType.HtmlSourceTypeCtrl._htmlCodeTbx.Text = settings.Html;
                            IBreadcrumbExplorerNextEntitySelected(this, htmlSourceType);
                        }
                        break;

                    case BrowserContentType.Url:
                        _sourceTypeCtrl._urlTypeRadio.IsChecked = true;
                        if (IBreadcrumbExplorerNextEntitySelected != null)
                        {
                            UrlSourceType urlSourceType = new UrlSourceType();
                            urlSourceType.Settings = settings;
                            urlSourceType.Name = "Url";
                            urlSourceType.UrlSourceTypeCtrl._urlTbx.Text = settings.Url;
                            IBreadcrumbExplorerNextEntitySelected(this, urlSourceType);
                        }
                        break;
                }
                
            }
        }

        public event IBreadcrumbExplorerNextEntitySelectedHandler IBreadcrumbExplorerNextEntitySelected;

        public System.Windows.Media.Imaging.BitmapImage Icon { get; set; }

        public string Name { get; set; }

        public void Update(object newValue)
        {
            
        }

        #endregion
    }
}
